package com.imonsoft.common.util;

import java.io.File;
import java.util.Date;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;

/**
 * PDF生成工具类，需要把doc/开发资料/jacob-1.17-M2.zip里面的DLL放入JDK的BIN下面方可执行
 * 
 * @author LiQianHui
 * @date 2013-5-31
 * @version 2013
 */
public class PDFUtil {

	protected final static Log logger = LogFactory.getLog(PDFUtil.class);

	static final int wdFormatPDF = 17;// PDF 格式

	/**
	 * word转换PDF
	 * 
	 * @param sfileName
	 * @param toFileName
	 */
	public void wordToPDF(String sfileName, String toFileName) {

		System.out.println("启动Word...");
		long start = System.currentTimeMillis();
		ActiveXComponent app = null;
		Dispatch doc = null;
		try {
			app = new ActiveXComponent("Excel.Application");
			app.setProperty("Visible", new Variant(false));
			Dispatch docs = app.getProperty("Documents").toDispatch();
			doc = Dispatch.call(docs, "Open", sfileName).toDispatch();
			System.out.println("打开文档..." + sfileName);
			System.out.println("转换文档到PDF..." + toFileName);
			File tofile = new File(toFileName);
			if (tofile.exists()) {
				tofile.delete();
			}
			Dispatch.call(doc, "SaveAs", toFileName, wdFormatPDF);
			long end = System.currentTimeMillis();
			System.out.println("转换完成..用时：" + (end - start) + "ms.");

		} catch (Exception e) {
			System.out.println("========Error:文档转换失败：" + e.getMessage());
		} finally {
			Dispatch.call(doc, "Close", false);
			System.out.println("关闭文档");
			if (app != null)
				app.invoke("Quit", new Variant[] {});
		}
		// 如果没有这句话,winword.exe进程将不会关闭
		ComThread.Release();
	}

	/**
	 * Excel转换PDF
	 * 
	 * @param filePath
	 * @param outFile
	 *            文件名不能有重复，如果有重复程序将为中断。
	 */
	public void excelToPDF(String filePath, String outFile) {
		ComThread.InitSTA();
		ActiveXComponent actcom = new ActiveXComponent("Excel.Application");
		try {
			System.out.println((new Date()).toString() + "  start convert from : " + filePath + " to " + outFile);
			actcom.setProperty("Visible", new Variant(false));
			Dispatch excels = actcom.getProperty("Workbooks").toDispatch();
			Dispatch excel = Dispatch.invoke(excels, "Open", Dispatch.Method, new Object[] { filePath, new Variant(false), new Variant(false) }, new int[9]).toDispatch();
			Dispatch.invoke(excel, "SaveAs", Dispatch.Method, new Object[] { outFile, new Variant(57), new Variant(false), new Variant(57), new Variant(57), new Variant(false), new Variant(true), new Variant(57), new Variant(false), new Variant(true), new Variant(false) }, new int[1]);
			Dispatch.call(excel, "Close", new Variant(false));
			if (actcom != null) {
				actcom.invoke("Quit", new Variant[] {});
				actcom = null;
			}
			ComThread.Release();
			System.out.println((new Date()).toString() + "  convert ok : " + filePath + " to " + outFile);
		} catch (Exception es) {
			es.printStackTrace();
		}
	}

	public static void main(String[] args) {
		PDFUtil d = new PDFUtil();
		// d.wordToPDF("D:\\General.xls", "D:\\test_321test.pdf");
		// d.aa("D:\\General.xls", "D:\\test_321test.pdf");
		d.excelToPDF("C:\\Users\\Administrator\\Desktop\\envManage.xls", "C:\\Users\\Administrator\\Desktop\\0公里投诉.xls");
	}
}
